package com.idconfirmsystem.parse;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.idconfirmsystem.model.Task;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

/**
 * @author WuXiaoming
 * @data:
 * @version: V1.0
 */
public class TaskParse {

	List<Map<String, Object>> listDates = new ArrayList<Map<String, Object>>();
	int listLength;

	public TaskParse(String result) {
		super();
		paeser(result);
	}

	public void setListDates(List<Map<String, Object>> listDate) {
		this.listDates = listDate;
	}

	public List<Map<String, Object>> getListDates() {
		return listDates;
	}

	public void setListLength(int length) {
		this.listLength = length;
	}

	public int getListLength() {
		return listLength;
	}

	public void paeser(String result) {
		// TODO Auto-generated method stub
		try {
			// File file = new File(Environment.getExternalStorageDirectory()
			// + "/IdConfirmData/TaskData/" + "20150822A.xml");
			// BufferedReader br = new BufferedReader(new InputStreamReader(
			// new FileInputStream(file), "utf-8"));
			// InputSource is = new InputSource(br);

			InputStream iStream = new ByteArrayInputStream(result.getBytes("GBK"));
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;
			db = dbf.newDocumentBuilder();
			Document document = db.parse(iStream); // 存放该xml文件的路径;
			Element root = document.getDocumentElement();// 获得根元素
			System.out.println("根元素：" + root.getNodeName());
			// Log.e("Task", "根元素11==>：" + root.getNodeName());
			NodeList rowList = document.getElementsByTagName("row");
			// Log.e("Task", "rowList: " + rowList.getLength());
			setListLength(rowList.getLength() - 9);
			for (int i = 9; i < rowList.getLength(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				NodeList info = rowList.item(i).getChildNodes();
				for (int j = 0; j < info.getLength(); j++) {
					// Log.e("Task", "info: " + info.getLength());
					Node infoNode = info.item(j);
					String nodesString = infoNode.getTextContent();
					// String nodesString =
					// infoNode.getTextContent();
					if ("aae314".equals(infoNode.getNodeName())) {
						Log.e("Task", "aae314: " + nodesString);
						map.put("aae314", nodesString);
					} else if ("aae317".equals(infoNode.getNodeName())) {
						Log.e("Task", "aae317: " + nodesString);
						map.put("aae317", nodesString);
					} else if ("id".equals(infoNode.getNodeName())) {
						Log.e("Task", "id: " + nodesString);
						map.put("id", nodesString);
					} else if ("line_number".equals(infoNode.getNodeName())) {
						Log.e("Task", "line_number: " + nodesString);
						map.put("line_number", nodesString);
					} else if ("car_name".equals(infoNode.getNodeName())) {
						Log.e("Task", "car_name: " + nodesString);
						map.put("car_name", nodesString);
					} else if ("pilot_name".equals(infoNode.getNodeName())) {
						String pilot = infoNode.getTextContent();
						Log.e("Task", "pilot_name: " + pilot);
						map.put("pilot_name", nodesString);
					} else if ("clerk_name".equals(infoNode.getNodeName())) {
						Log.e("Task", "cl rk_name: " + nodesString);
						map.put("clerk_name", nodesString);
					} else if ("escort_name1".equals(infoNode.getNodeName())) {
						Log.e("Task", "escort_name1: " + nodesString);
						map.put("escort_name1", nodesString);
					} else if ("escort_name2".equals(infoNode.getNodeName())) {
						Log.e("Task", "escort_name2: " + nodesString);
						map.put("escort_name2", nodesString);
					}
				}
				listDates.add(map);
			}
			setListDates(listDates);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}